var checkedNodes = [];
layui.use(['element', 'table', 'form', 'layer', 'upload','layedit'], function(){
    var element = layui.element;
    var $ = layui.$;
    var form = layui.form;
    var table = layui.table;
    var layer = layui.layer;
    var upload = layui.upload;
    var layedit = layui.layedit;
    //获取创建人id
    var userId = localStorage.getItem('createId');
    $('input[name=userId]').val(userId);
    var layedits = layui.layedit;
    layedit.set({	//设置图片接口
        uploadImage: {
            url: '/micro/layeditUpload', //接口url
            type: 'post'
        }
    });
    var build = layedits.build('demo'); //建立编辑器
    var uploadInst = upload.render({
        elem: "#test",
        url: "/upload/uploadFile",
        multiple: true,
        size:2248,
        field: "files",
        accept: "images",
        before: function (obj) {
            $('#images').show();
        },
        done: function (res, index, upload) {
            var mainImage = res.data.path;
            $('#image').append('<img src="' + res.data.path
                + '" height="100px" width="100px" border="1px solid #ccc" class="layui-upload-img uploadImgPreView">');
            $('input[name=artcleUrl]').val(mainImage);
        },
        error: function () {
            //演示失败状态，并实现重传
            var demoText = $('#demoText');
            demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
            demoText.find('.demo-reload').on('click', function () {
                uploadInst.upload();
            });
        }, progress: function (n, elem, e) {
            element.progress('demo', n + '%');
            if (n == 100) {
                layer.msg('上传完毕', {icon: 1});
            }
        }
    });

    //微圈分类
    findType = function findType() {

        layer.open({
            type: 1,
            title: '选择分类',
            area: ['500px', '400px'],
            maxmin: true,
            shade: 0,
            offset: 'auto',
            content: '<ul id="ztree" class="ztree"></ul>',
            btn: ['确定', '取消'],
            yes: function (index, layero) {
                $('input[name=artcleCategroyName]').val(artcleCategroyName);
                $('input[name=acticleCategroyId]').val(acticleCategroyId);
                $('#sort').show();
                layer.close(index);
            },
            btn2: function (index, layero) {

            }
        });
        var settingss = {
            data: {
                simpleData: {
                    enable: true,  //true 、 false 分别表示 使用 、 不使用 简单数据模式
                    idKey: "acticleCategroyId",   //节点数据中保存唯一标识的属性名称
                    pIdKey: "artcleCatefroyParentId",    //节点数据中保存其父节点唯一标识的属性名称
                    rootPId: -1  //用于修正根节点父节点数据，即 pIdKey 指定的属性值
                },
                key: {
                    name: "artcleCategroyName"  //zTree 节点数据保存节点名称的属性名称  默认值："name"
                }
            },
            check: {
                enable: true,
                chkStyle: 'checkbox',
                nocheckInherit: true   //当父节点设置 nocheck = true 时，设置子节点是否自动继承 nocheck = true
            },
            callback: {
                onCheck: onCheck
            }
        };
        //点击节点
        var artcleCategroyName = "";
        var acticleCategroyId = "";
        function onCheck(e, treeId, treeNode) {
            var treeObj = $.fn.zTree.getZTreeObj("ztree"),
                nodes = treeObj.getCheckedNodes(true),
                v = "";
            artcleCategroyName = "";
            acticleCategroyId = "";
            for (var i = 0; i < nodes.length; i++) {
                v += nodes[i].artcleCategroyName + ">";
                artcleCategroyName = nodes[i].artcleCategroyName;
                acticleCategroyId = nodes[i].acticleCategroyId;
                checkedNodes += nodes[i].acticleCategroyId+'';
            }
            var leng = v.substring(v.indexOf('>')+1);
            v = leng.substring(0,leng.length-1)
            $('#type').text(v);
        }
        //加载树的数据
        $.ajax({
            url:"/micro/getAllzTree",
            type: 'post',
            dataType: 'json',
            success: function (data) {
                zTreeObj = $.fn.zTree.init($("#ztree"), settingss, data.data); //初始化树
                zTreeObj.expandAll(true);   //true 节点全部展开、false节点收缩
                // 回显选中的数据
                var treeObj = $.fn.zTree.getZTreeObj("ztree");
                for(var i = 0; i < checkedNodes.length; i++) {
                    var rnode = treeObj.getNodeByParam("acticleCategroyId", checkedNodes[i]);
                    treeObj.checkNode(rnode,true);
                }
            }
        });
    }

    form.on('submit(sub)',function (data) {
        var paramProduct = $('#form').serialize();
        var content = layedit.getContent(build);//富文本内容
        var data = paramProduct + content;
        $.ajax({
            url:'/add-micro/addMicro',
            data:data,
            dataType:'json',
            success:function (data) {
                layer.msg("添加成功")
            }
        });
    });

    //返回
    goBack = function goBack() {
        parent.$('#beforeHref').val($('.layui-elem-quote').children('span').eq(0).text());
        parent.before();
        return false;
    }
});